﻿package app.view.components.sections.expo.max 
{
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.ApplicationMediator;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.PopupHeadDBase;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	/**
	 * 1.1.2.2 КАРТА И МАРШРУТ
	 * firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class ExpoPavilionRouteMBase extends LabeledSectionBase
	{
		// app.view.components.sections.expo.max.ExpoPavilionRouteMBase
		
		public var routeProhod_Btn:GlamurBtnBase;
		public var takeObject_Btn:GlamurBtnBase;
		
		public var narrow_route:MovieClip;
		
		private var _isTimer:Boolean=false;
		private var _timer:Timer;
		
		private var _currentPopups:PopupHeadDBase;
		private var _itemsPopups:/*PopupHeadDBase*/Array;
		
		public function ExpoPavilionRouteMBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			
			_timer = new Timer(250);
			
			narrow_route.alpha = 0;
			routeProhod_Btn.bgdSelected.alpha = 0;
		}
		
		private function setEvents():void
		{
			routeProhod_Btn.addEventListener(MouseEvent.CLICK, onClickRouteProhod);
			takeObject_Btn.addEventListener(MouseEvent.CLICK, onClickTakeObject);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
			
			addEventListener(UIEvent.CLICK_MINI_POPUPS, onClickMiniPopups, true);
		}
		
		
		private function removeEvents():void
		{
			routeProhod_Btn.removeEventListener(MouseEvent.CLICK, onClickRouteProhod);
			takeObject_Btn.removeEventListener(MouseEvent.CLICK, onClickTakeObject);
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER, onTimer);
			
			removeEventListener(UIEvent.CLICK_MINI_POPUPS, onClickMiniPopups, true);
		}
		
		private function createPopups():void
		{
			clearPopupsArray();
			
			//trace('+++',_datapopup.length);
			
			for (var i:int = 0; i < _datapopup.length; i++) 
			{
				//trace('***',_datapopup[i]);
				var item:PopupHeadDBase = new PopupHeadDBase(i);
				addChild(item);
				item.setParamContent(_datapopup[i].name, _datapopup[i].text, _datapopup[i].image);
				
				
				item.x = ApplicationMediator.SCREEN_WIDTH*Number(_datapopup[i].posx)/100;
				item.y = ApplicationMediator.SCREEN_HEIGHT*Number(_datapopup[i].posy)/100;
				
				/*trace('+++', _datapopup[i].posx, _datapopup[i].posy);
				trace('***x',item.x , area.x);
				trace('***y',item.y , area.y);*/
				_itemsPopups.push(item);
			}
			
		}
		
		private function clearPopupsArray():void
		{
			// чистим	
			if (_itemsPopups) 
			{
				for (var j:int = 0; j < _itemsPopups.length; j++) 
				{
					_itemsPopups[j].destroy();
					removeChild(_itemsPopups[j]);
				}
				
				var len:int = _itemsPopups.length;
				for (var i:int = 0; i < len; i++) 
				{
					_itemsPopups.pop();
				}
				_currentPopups = null;
			}
			
			_itemsPopups = new Array();
		}
		
		/**
		 * установить выделенную линию
		 */
		public function setSelectedItemPopups(ind:int):void
		{
			if (_currentPopups==null) 
			{
				_currentPopups = _itemsPopups[ind];
				_currentPopups.selected = true;
			}else
			{
				_currentPopups.selected = false;
				_currentPopups = _itemsPopups[ind];
				_currentPopups.selected = true;
			}
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			routeProhod_Btn.destroy();
			takeObject_Btn.destroy();
			
			clearPopupsArray();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			
			
			setParamHead(HeadMarkerBase.BGD_RED);
			
			routeProhod_Btn.setLang(_data.lang);
			takeObject_Btn.setLang(_data.lang);
			
			setEvents();
		};
		
		override public function get datapopup():Object { return _datapopup; }
		
		override public function set datapopup(value:Object):void 
		{
			super.datapopup = value;
			
			//trace('datapopup', _datapopup.length);
			if (_datapopup) 
			{
				createPopups();
			}
			
		}
		
		/**
		 * 
		 * @param	e
		 */
		private function onTimer(e:TimerEvent):void 
		{
			
			if (narrow_route.alpha==0) 
			{
				narrow_route.alpha = 1;
			}else
			{
				narrow_route.alpha = 0;
			}
			
		}
		
		
		/**
		 * показываем маршрут прохода
		 * @param	e
		 */
		private function onClickRouteProhod(e:MouseEvent):void 
		{
			
			if (_isTimer==false) 
			{
				_isTimer = true;
				_timer.start();
				routeProhod_Btn.bgdSelected.alpha = 1;
			}else
			{
				_isTimer = false;
				_timer.stop();
				narrow_route.alpha = 0;
				routeProhod_Btn.bgdSelected.alpha = 0;
			}
			
		}
		
		/**
		 * это попап с указателем объектов
		 * @param	e
		 */
		private function onClickTakeObject(e:MouseEvent):void 
		{
			clearPopupsArray();
			
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.POPUP_EXPO_OBJECTS;
			obj.data = _data.data.cat[2];
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_OPEN);
			ev.data = obj;
			dispatchEvent(ev);
			
		}
		
		/**
		 * 
		 * @param	e
		 */
		private function onClickMiniPopups(e:UIEvent):void 
		{
			//trace('+++',e.id);
			
			setSelectedItemPopups(e.id);
		}
	}

}